package elitech.vietnam.myfashion.view;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView2;
import com.huewu.pla.lib.MultiColumnListView;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.extend.GridViewFarvoriteArrayAdapter;

public class FavoritesListView extends LinearLayout implements OnRefreshListener2<MultiColumnListView> {

	private static final int NUM_ITEMS = 10;
	
	private MainActivity mContext;
	private PullToRefreshGridView2 mPullRefreshGridView;
//	private MultiColumnListView mGridView;
	private GridViewFarvoriteArrayAdapter mAdapter;
	private int countItems = 0;
	
	public FavoritesListView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.favoriteslist, this);
		}
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		mPullRefreshGridView = (PullToRefreshGridView2) this.findViewById(R.id.favor_gridview);
		mPullRefreshGridView.setMode(Mode.PULL_FROM_END);
		mPullRefreshGridView.setOnRefreshListener(this);
//		mGridView = mPullRefreshGridView.getRefreshableView();
		mPullRefreshGridView.setPadding(0, 1, 1, 0);
		mAdapter = new GridViewFarvoriteArrayAdapter(mContext, R.id.favor_gridview, 0, mContext.mListFavorite);
		mPullRefreshGridView.setAdapter(mAdapter);	
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		if (mContext.mListFavorite.size() == 0)
			GetFavorite();
	}

	public void onResume() {
		if (mPullRefreshGridView.isRefreshing())
			mPullRefreshGridView.onRefreshComplete();
	}

	public void GetFavorite() {
		new CallServiceTask() {
			private ArrayList<Product> rs = new ArrayList<Product>();
			@Override
			public void work() {
				try {
					rs = service.getListFavorite(mContext.mUser.Id, mContext.mListFavorite.size(), NUM_ITEMS);
				} catch (IOException e) {
					Log.e("Shopping", e.toString());
				} catch (XmlPullParserException e) {}
			}
			@Override
			protected void onPostExecute(Void result) {
				if(rs!=null){
					mContext.mListFavorite.addAll(rs);
					countItems = countItems + rs.size();
				}
				mAdapter.notifyDataSetChanged();
				mPullRefreshGridView.onRefreshComplete();
			}
		}.execute();
	}

	public void RefreshFavorite() {
		new CallServiceTask() {
			private ArrayList<Product> rs = new ArrayList<Product>();
			@Override
			public void work() {
				try {
					int numb = (countItems>NUM_ITEMS)? countItems:NUM_ITEMS;
					rs = service.getListFavorite(mContext.mUser.Id, 0, numb);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {}
			}
			@Override
			protected void onPostExecute(Void result) {
				if(rs!= null){
					mContext.mListFavorite.clear();
					mContext.mListFavorite.addAll(rs);
					countItems = rs.size();
				}
				mAdapter.notifyDataSetChanged();
				mPullRefreshGridView.onRefreshComplete();
			}
		}.execute();
	}
	
	@Override
	public void onPullDownToRefresh(
			PullToRefreshBase<MultiColumnListView> refreshView) {
	}

	@Override
	public void onPullUpToRefresh(
			PullToRefreshBase<MultiColumnListView> refreshView) {
		GetFavorite();
	}

}
